package com.atguigu.gmall.payment.receiver;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.payment.service.PaymentService;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author Fxz
 * @version 1.0
 * @date 2022-02-24 21:10
 */
@Component
@RequiredArgsConstructor
public class PaymentReceiver {

	private final PaymentService paymentService;

	@SneakyThrows
	@RabbitListener(bindings = @QueueBinding(
			value = @Queue(value = MqConst.QUEUE_PAYMENT_CLOSE, durable = "true", autoDelete = "false"),
			exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_PAYMENT_CLOSE),
			key = { MqConst.ROUTING_PAYMENT_CLOSE }))
	public void closePaymentInfo(Long orderId, Message message, Channel channel) {
		if (orderId != null) {
			// 关闭交易记录
			paymentService.closePayment(orderId);
		}
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}

}
